package com.bc.trade.model;

import com.bc.common.basemodel.BaseWxClear;
import com.bc.common.message.WxgjClearingResp;
import com.fs.common.util.FSIDGenerate;

@SuppressWarnings("serial")
public class WxClear extends BaseWxClear<WxClear>{
	public static final WxClear dao = new WxClear().dao();
	
	public static void createClear(WxgjClearingResp resp, String subMerchCode) {
		WxClear wxClear = new WxClear();
		wxClear.setDateEnd(resp.getDate_end());
		wxClear.setDateStart(resp.getDate_start());
		wxClear.setDateSettlement(resp.getDate_settlement());
		wxClear.setFbatchno(resp.getFbatchno());
		wxClear.setId(FSIDGenerate.getUUID());
		wxClear.setMerchCode(resp.getMerchCode());
		wxClear.setOrgCode(resp.getOrgCode());
		wxClear.setPayFee(Long.parseLong(resp.getPay_fee()));
		wxClear.setPayNetFee(Long.parseLong(resp.getPay_net_fee()));
		wxClear.setPoundageFee(Long.parseLong(resp.getPoundage_fee()));
		wxClear.setRefundFee(Long.parseLong(resp.getRefund_fee()));
		wxClear.setSettlementFee(Long.parseLong(resp.getSettlement_fee()));
		wxClear.setSettlementfeeType(resp.getSettlementfee_type());
		wxClear.setUnsettlementFee(Long.parseLong(resp.getUnsettlement_fee()));
		wxClear.setWxSubMerchCode(subMerchCode);
		
		WxClear tmpWx = WxClear.dao.findFirst("SELECT * FROM wx_clear WHERE merchCode='" + resp.getMerchCode()
		  + "' AND " + "fbatchno='" + resp.getFbatchno() + "' AND " +
				"date_settlement='" + resp.getDate_settlement() + "'");
		
		if(tmpWx == null || tmpWx.getFbatchno()==null) {
			wxClear.save();
		}
	}
	
	public WxClear selectMaxEndDate(String merchCode) {
		String minDate = "SELECT MAX(date_settlement) FROM wx_clear WHERE merchCode='" + merchCode + "'";
		
		return WxClear.dao.findFirst("SELECT * FROM wx_clear WHERE merchCode='" + merchCode + "'"
				+ " AND " + "date_settlement=(" + minDate + ")");
		
	}
	
}
